package com.example.market;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Search_Activity extends AppCompatActivity {

    EditText serch;

    ProgressBar progressBar2;

    private List<ImageItem> imageItems=new ArrayList<>();
    private SerchAdapter adapter;

    private List<String> apps;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        serch = findViewById(R.id.serch);


        apps = Arrays.asList("天气", "应用市场", "空调", "音乐播放", "用户中心", "疲劳驾驶", "360全景");



        progressBar2 = findViewById(R.id.progressBar2);


        ListView listView = findViewById(R.id.serchlv);


        serch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                String text = s.toString();
                List<String> name = searchApp(text);
                Log.v("name",String.valueOf(name));



                if (String.valueOf(name).equals("[天气]")) {
                    imageItems.add(new ImageItem(R.drawable.weather, "天气"));

                } else if (String.valueOf(name).equals("[应用市场]")) {
                    imageItems.add(new ImageItem(R.drawable.market, "应用市场"));
                } else if (String.valueOf(name).equals("[空调]")) {
                    imageItems.add(new ImageItem(R.drawable.air, "空调"));
                } else if (String.valueOf(name).equals("[音乐播放]")) {
                    imageItems.add(new ImageItem(R.drawable.music, "音乐播放"));
                } else if (String.valueOf(name).equals("[用户中心]")) {
                    imageItems.add(new ImageItem(R.drawable.user, "用户中心"));
                } else if (String.valueOf(name).equals("[疲劳驾驶]")) {
                    imageItems.add(new ImageItem(R.drawable.drive, "疲劳驾驶"));
                } else if (String.valueOf(name).equals("[360全景]")) {
                    imageItems.add(new ImageItem(R.drawable.quanjing, "360全景"));
                }


                adapter = new SerchAdapter();
                listView.setAdapter(adapter);


                if (serch.getText().toString().equals("")) {
                    listView.setVisibility(View.GONE);
                    if (imageItems.size()!=0){
                      imageItems.clear();

                    }

                } else {
                    listView.setVisibility(View.VISIBLE);
                }
            }
        });


    }








    private List<String> searchApp(String text) {
        List<String> appname = new ArrayList<>();

        for (String app : apps) {
            if (app.contains(text)) {
                appname.add(app);
            }
        }
        return appname;
    }


    private class SerchAdapter extends BaseAdapter {

        public int getCount() {
            return imageItems.size();
        }


        public Object getItem(int position) {

            return imageItems.get(position);
        }


        public long getItemId(int position) {
            return position;
        }


        public View getView(int position, View view, ViewGroup viewGroup) {

            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.update, viewGroup, false);

            ImageView imgLogo = view.findViewById(R.id.imgLogo);
            TextView txt_name = view.findViewById(R.id.txt_name);

           Button btn_update=view.findViewById(R.id.btn_update);
           btn_update.setText("下载");


            btn_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Toast.makeText(Search_Activity.this, "正在下载。。。", Toast.LENGTH_SHORT).show();
                        progressBar2.setVisibility(View.VISIBLE);
                        new DownloadTask().execute();

                }

            });

            ImageItem item = imageItems.get(position);
            imgLogo.setImageResource(item.getImageView());
            txt_name.setText(item.getTextname());



            return view;
        }
    }

    private class DownloadTask extends AsyncTask<Void,Integer,Void> {

        @Override
        protected void onPreExecute() {
            
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            for (int i = 0; i < 100; i++) {

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                publishProgress(i);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            
            progressBar2.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Void unused) {
            super.onPostExecute(unused);
            
            progressBar2.setVisibility(View.GONE);
            Toast.makeText(Search_Activity.this, "下载完成", Toast.LENGTH_SHORT).show();
        }
    }
}